package com.fastadmin.pojo;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.fastadmin.common.mapper.JsonMapper;
import com.google.common.base.Splitter;
import lombok.Data;
import org.asynchttpclient.util.StringBuilderPool;

import java.text.DateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

@Data
public class FastParam {

    public static final JsonMapper JSON_MAPPER = JsonMapper.nonDefaultMapper();

    public String sort = "id";
    public String order = "DESC";
    public String filter;
    public String op;
    public Integer offset = 0;
    public Integer limit = 10;


    public String getSql(String tableName) {
        StringBuilder sql = StringBuilderPool.DEFAULT.stringBuilder();
        sql.append("SELECT * FROM ");
        sql.append(tableName);
        sql.append(" WHERE 1=1 ");
        if (StrUtil.isNotBlank(filter)) {
            Map<String, Object> filterMap = JSON_MAPPER.fromJson(this.filter, Map.class);
            Map<String, Object> opMap = JSON_MAPPER.fromJson(this.op, Map.class);
            for (Entry<String, Object> entry : filterMap.entrySet()) {
                String key = entry.getKey();
                String op = opMap.get(key).toString();
                String value = StrUtil.trim(entry.getValue().toString());
                if (op.equals("=")) {
                    sql.append(" AND  " + key + " = '" + value + "' ");
                }
                if (StrUtil.containsIgnoreCase(op, "in")) {
                    String[] split = StrUtil.split(value, ",");
                    String[] strings = StrUtil.wrapAll("'", "'", split);
                    sql.append(" AND  " + key + " IN (" + StrUtil.join(",", strings) + ") ");
                }
                if (StrUtil.containsIgnoreCase(op, "like")) {
                    sql.append(" AND  " + key + " LIKE '%" + value + "%' ");
                }
                if (StrUtil.containsIgnoreCase(op, "between")) {
                    List<String> betweens = Splitter.on(",").splitToList(value);
                    String start = convertTimeToString(betweens.get(0));
                    String end = convertTimeToString(betweens.get(1));

                    sql.append(" AND  " + key + " BETWEEN  '" + start + "' AND '" + end + "' ");
                }
            }
        }
        sql.append(" ORDER BY " + sort + " " + order + "");
        return StrUtil.trim(sql.toString());
    }
    /**
     * 将Long类型的时间戳转换成String 类型的时间格式，时间格式为：yyyy-MM-dd HH:mm:ss
     * @param time
     */
    public static String convertTimeToString(String time){
        Assert.notNull(time, "time is null");
        DateTimeFormatter ftf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return ftf.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(Long.parseLong(time) * 1000), ZoneId.systemDefault()));
    }

    public Map<String, Object> getFilterMap() {
        if (StrUtil.isNotBlank(filter)) {
            Map<String, Object> filterMap = JSON_MAPPER.fromJson(this.filter, Map.class);
            return filterMap;
        }
        return null;
    }

}
